<?php

class Services_Model
{
	/**
	 * Holds instance of database connection
	 */
	private $db;
		
	public function __construct()
	{
		$this->db = new MysqlImproved_Driver;
	}
	
	public function get_article($link_title)
	{		
		//connect to database
		$this->db->connect();
		$link_title = $this->db->escape($link_title);
		$this->get_page_data();
		$article['page_title'] = $this->page_data['title'];
		$article['page_description'] = $this->page_data['description'];
		$article['content'] = $this->get_service_data($link_title);
		$article['description'] = $this->get_service_description($link_title);
		$article['keywords'] = $this->get_service_keywords($link_title);
		return $article;
	}
	
	public function get_service_data($link_title){
		$this->db->prepare(
				"
				SELECT
				`service_title`,
				`service_link`,
				`service_description`,
				`service_content`,
				`service_image`
				FROM
				`services`
				WHERE
				`service_link` = '$link_title'
				LIMIT
				1
				;
				"
				);
		$this->db->query();
		$service_content = "";
		while($service_data = $this->db->fetch('array')){
			$service_content .= "
					<!-- Begin Service Item -->
				
					<div class=\"service-img-wrap\">
						<a class=\"lightbox\" href=\"../images/services/".$service_data['service_image']."\"><img class=\"service-img\" src=\"../images/services/".$service_data['service_image']."\" alt=\"".$service_data['service_title']."\" /></a>
					</div>
					<h3 class=\"service-h left\">".$service_data['service_title']."</h3>
					<div class=\"service-parag clear\">
						".$service_data['service_content']."
					</div>
					
				<!-- End Service Item -->
			";
		}
		return $service_content;
	}
	
	public function get_service_description($link_title){
		
		$this->db->prepare(
				"
				SELECT
				`service_meta_description`
				FROM
				`services`
				WHERE
				`service_link` = '$link_title'
				LIMIT
				1
				;
				"
		);
		$this->db->query();
		while($service_data = $this->db->fetch('array')){
			$service_description = $service_data['service_meta_description'];
		}
		return $service_description;
	}
	
	public function get_service_keywords($link_title){
		$this->db->prepare(
				"
				SELECT
				`service_keywords`
				FROM
				`services`
				WHERE
				`service_link` = '$link_title'
				LIMIT
				1
				;
				"
		);
		$this->db->query();
		while($service_datas = $this->db->fetch('array')){
			$service_keywords = $service_datas['service_keywords'];
		}
		return $service_keywords;
	}
	
	public function get_page_data(){
		//prepare query
		$this->db->prepare
		(
				"
				SELECT
				`page`,
				`title`,
				`description`
				FROM
				`page_description`
				WHERE
				`page` = 'services'
				LIMIT
				1
				;
				"
		);
	
		//execute query
		$this->db->query();
	
		$this->page_data = $this->db->fetch('array');
		return $this->page_data;
	}
	
}

